//***********************************************************************
//	File:		MainMenu.cpp
//	Author:		
//	Course:		SGP 1405
//	Purpose:	
//***********************************************************************
//***********************************************************************
//	Purposfully left blank for all comments regarding this file 
//	
//	
//	
//***********************************************************************


#include <fstream>
#include "MainMenu.h"

#include "../SGD Wrappers/SGD_AudioManager.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_InputManager.h"

#include "Options.h"
#include "Instructions.h"
#include "Credits.h"
#include "Play.h"
#include "LoadGame.h"


#include "../source/Game.h"
#include "../source/BitmapFont.h"
#include "../TileSystem/TileSystem.h"

#include "../TinyXML/tinyxml.h"

/**************************************************************/
// GetInstance
/*static*/ MainMenu* MainMenu::GetInstance(void)
{
	static MainMenu s_Instance;

	return &s_Instance;
}

/**************************************************************/
// Enter
void MainMenu::Enter(void)
{
	// Volume Control
	unsigned int masterVolume = 0; 
	unsigned int sfxVolume = 0; 

	// Set the background (we will be changing this later)
	SGD::GraphicsManager::GetInstance()->SetClearColor({ 50, 50, 50 });

}

/**************************************************************/
// Exit
//	- deallocate / unload resources
void MainMenu::Exit(void)
{

}


bool MainMenu::Input(void)
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance(); 
	Game* joyStickCoolDown = Game::GetInstance(); 

	 joystick0Pos = pInput->GetLeftJoystick(0); 
	 joystick1Pos = pInput->GetLeftJoystick(1); 

	if (joystick1Pos.x > 0 || joystick1Pos.x < 0 ||
		joystick1Pos.y > 0 || joystick1Pos.y < 0)
	{
		Game::GetInstance()->SetController_1_Left_Joystick_Cooldown(true);
	}
	else
	{
		Game::GetInstance()->SetController_1_Left_Joystick_Cooldown(false);
	}

	// Press Escape to quit the game
	if (pInput->IsKeyPressed(SGD::Key::Escape) == true || 
		pInput->IsButtonPressed(0, 1) == true || 
		pInput->IsButtonPressed(1,1))
		m_nCursor = m_nCursorMax;

	if (pInput->IsKeyPressed(SGD::Key::Down) == true || 
		pInput->IsDPadPressed(0, SGD::DPad::Down) == true || 
		pInput->IsDPadPressed(1, SGD::DPad::Down) == true || 
		(!joyStickCoolDown->GetController_0_Left_Joystick_Cooldown() && joystick0Pos.y > 0 ) ||
		(!joyStickCoolDown->GetController_1_Left_Joystick_Cooldown() && joystick1Pos.y > 0))

	{

		++m_nCursor; 

		// give it the old Wrap around
		if (m_nCursor > m_nCursorMax)
			m_nCursor = 0; 
	}
	else if (pInput->IsKeyPressed(SGD::Key::Up) == true 
		|| pInput->IsDPadPressed(0,SGD::DPad::Up) == true 
		|| pInput->IsDPadPressed(1,SGD::DPad::Up) == true)
	{
		--m_nCursor;

		// give it the old Wrap around
		if (m_nCursor < 0)
			m_nCursor = m_nCursorMax;
	}
	else if (pInput->IsKeyPressed(SGD::Key::Enter) == true ||
		pInput->IsButtonPressed(0, 0) == true || 
		pInput->IsButtonPressed(1,0) == true)
	{
		switch (m_nCursor)
		{
		case 0:
		{
				  Game::GetInstance()->ChangeState(LoadGame::GetInstance());
				  return true;
		};
		case 1:
		{
				  Game::GetInstance()->ChangeState(Instructions::GetInstance());
				  return true; 
		};
		case 2:
		{
				  Game::GetInstance()->ChangeState(Options::GetInstance());
				  return true; 
		}
		case 3:
		{
				  Game::GetInstance()->ChangeState(Credits::GetInstance());
				  return true; 
		}
		case 4:
		{
				  Game::GetInstance()->ChangeState(nullptr);
				  return false;
		}

		};
	}

	

	if (joystick0Pos.x > 0 || joystick0Pos.x < 0 ||
		joystick0Pos.y > 0 || joystick0Pos.y < 0)
	{
		Game::GetInstance()->SetController_0_Left_Joystick_Cooldown(true);
	}
	else if (joystick0Pos.x == 0 && joystick0Pos.y == 0 &&
		joyStickCoolDown->GetController_0_Left_Joystick_Cooldown() == true)
	{
		Game::GetInstance()->SetController_0_Left_Joystick_Cooldown(false);
	}

	if (joystick0Pos.x < 0.25f && joystick0Pos.x >-0.25f)
	{
		joystick0Pos.x = 0;
	}
	if (joystick0Pos.y < 0.25f && joystick0Pos.y >-0.25f)
	{
		joystick0Pos.y = 0;
	}

	return true;
}

/**************************************************************/
// Update
//	- update entities / animations
/*virtual*/ void MainMenu::Update(float elapsedTime)
{
}

/**************************************************************/
// Render
//	- render entities / menu options
void MainMenu::Render(void)
{

	

	BitmapFont* pFont = Game::GetInstance()->GetFont();
	float width = Game::GetInstance()->GetScreenWidth(); 
	float height = Game::GetInstance()->GetScreenHeight(); 


	
	pFont->Draw("Bullet Bros", (int)(width / 3.0f - 42), 50, 2, { 255, 255, 200, 0 });
	
	// Options
	if (m_nCursor == PLAY)
		pFont->Draw("Play", (int)(width / 2.0f - 32), 200, 1, { 255, 255, 0, 0 });
	else 
		pFont->Draw("Play", (int)(width / 2.0f - 32),200, 1, { 255, 255, 200, 0 });

	if (m_nCursor == INSTRUCTIONS)
		pFont->Draw("Instructions", (int)(width / 2.0f - 96), 250, 1, { 255, 255, 0, 0 });
	else
		pFont->Draw("Instructions", (int)(width / 2.0f - 96), 250, 1, { 255, 255, 200, 0 });

	if (m_nCursor == OPTIONS_STATE)
		pFont->Draw("Options", (int)(width / 2.0f - 64), 300, 1, { 255, 255, 0, 0 });
	else
		pFont->Draw("Options", (int)(width / 2.0f - 64), 300, 1, { 255, 255, 200, 0 });

	if (m_nCursor == CREDITS)
		pFont->Draw("Credits", (int)(width / 2.0f - 64), 350, 1, { 255, 255, 0, 0 });
	else
		pFont->Draw("Credits", (int)(width / 2.0f - 64), 350, 1, { 255, 255, 200, 0 });
	
	if (m_nCursor == EXIT)
		pFont->Draw("Exit", (int)(width / 2.0f - 32), 400, 1, { 255, 255, 0, 0 });
	else
		pFont->Draw("Exit", (int)(width / 2.0f - 32), 400, 1, { 255, 255, 200, 0 });



}